//
//  StringExtension.swift
//  yunshang
//
//  Created by zw on 2019/5/31.
//  Copyright © 2019 zw. All rights reserved.
//

import UIKit

extension String {
    func ga_widthForComment(fontSize: CGFloat, height: CGFloat = 15) -> CGFloat {
        let font = UIFont.systemFont(ofSize: fontSize)
        let rect = NSString(string: self).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return ceil(rect.width)
    }
    
    func ga_heightForComment(fontSize: CGFloat, width: CGFloat) -> CGFloat {
        let font = UIFont.systemFont(ofSize: fontSize)
        let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return ceil(rect.height)
    }
    
    func ga_heightForComment(fontSize: CGFloat, width: CGFloat, maxHeight: CGFloat) -> CGFloat {
        let font = UIFont.systemFont(ofSize: fontSize)
        let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return ceil(rect.height)>maxHeight ? maxHeight : ceil(rect.height)
    }
    
    static func convertDictionaryToString(dict:[String:Any]) -> String {
        var result:String = ""
        do {
            //如果设置options为JSONSerialization.WritingOptions.prettyPrinted，则打印格式更好阅读
            let jsonData = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.init(rawValue: 0))
            
            if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
                result = JSONString
            }
            
        } catch {
            result = ""
        }
        return result
    }
    
    static func convertArrayToString(arr:[AnyObject]) -> String {
        var result:String = ""
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: arr, options: JSONSerialization.WritingOptions.init(rawValue: 0))
            
            if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
                result = JSONString
            }
            
        } catch {
            result = ""
        }
        return result
    }
    
    func convertStringToDictionary() -> [String:AnyObject]? {
        if let data = self.data(using: String.Encoding.utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: [JSONSerialization.ReadingOptions.init(rawValue: 0)]) as? [String:AnyObject]
            } catch let error as NSError {
                print(error)
            }
        }
        return nil
    }
    
    func deleteControlCharacter(oriString:String)-> String {
        let ocStr = oriString as NSString
        let ocMStr = NSMutableString.init(string: ocStr)
        let set = CharacterSet.controlCharacters
        var range = ocMStr.rangeOfCharacter(from: set)
        while range.location != NSNotFound {
            ocMStr.deleteCharacters(in: range)
            range = ocMStr.rangeOfCharacter(from: set)
        }
        let resultStr = ocMStr as String
        return resultStr
    }
}


extension String {
    
    var unicodeStr:String {
        
        let tempStr1 = self.replacingOccurrences(of: "\\u", with: "\\U")
        
        let tempStr2 = tempStr1.replacingOccurrences(of: "\"", with: "\\\"")
        
        let tempStr3 = "\"".appending(tempStr2).appending("\"")
        
        let tempData = tempStr3.data(using: String.Encoding.utf8)
        
        var returnStr:String = ""
        
        do {
            
            returnStr = try PropertyListSerialization.propertyList(from: tempData!, options: [.mutableContainers], format: nil) as! String
            
        } catch {
            
            print(error)
            
        }
        
        return returnStr.replacingOccurrences(of: "\\r\\n", with: "\n")
        
    }
    
}
